//
//  WBEmoticonTool.swift
//  WeiBo
//
//  Created by 韩菲菲 on 16/8/19.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

import UIKit

let WBEmoticonsMaxCol = 7
let WBEmoticonsMaxRow = 3
let WBEmoticonsMaxCount = WBEmoticonsMaxCol * WBEmoticonsMaxRow - 1

class WBEmoticonTool: NSObject {
    
    // MARK: - 单例设计模式
    static let sharedEmoticonTool:WBEmoticonTool = WBEmoticonTool()
    
    //定义一个bundle属性 可以获取到bundle
    let emoticonBundle: NSBundle = {
        
        //先获取bundle路径
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        //通过路径获取Bundle
        return NSBundle(path: path)!
        
    }()
    
    //定义default图片属性 一维数组
    lazy var defaultEmoticons:[WBEmoticonModel] = {
        
        return self.getEmoticonsWithPath("default")
        
    }()
    //定义default图片属性 一维数组
    lazy var emojiEmoticons:[WBEmoticonModel] = {
        
        return self.getEmoticonsWithPath("emoji")
        
    }()
    //定义default图片属性 一维数组
    lazy var lxhEmoticons:[WBEmoticonModel] = {
        
        return self.getEmoticonsWithPath("lxh")
        
    }()
    
    //定义三维数组
    lazy var allEmoticons:[[[WBEmoticonModel]]] = {
    
      return  [
        self.getEmoticonsGroup(self.defaultEmoticons),
        self.getEmoticonsGroup(self.emojiEmoticons),
        self.getEmoticonsGroup(self.lxhEmoticons)
        ]
    }()
    
    
    // MARK: - 根据传进来的一维数组 转变为二维数组
    func getEmoticonsGroup(emoticons:[WBEmoticonModel]) -> [[WBEmoticonModel]]{
        //看看有多少页
        let page = (emoticons.count + WBEmoticonsMaxCount - 1) / WBEmoticonsMaxCount
        
        //创建临时的二维数组
        var tempArray:[[WBEmoticonModel]] = [[WBEmoticonModel]]()
        //遍历页数
        for i in 0..<page{
            
            let location = i * WBEmoticonsMaxCount
            
            var length = WBEmoticonsMaxCount
            
            if location + length > emoticons.count{
                
                length = emoticons.count - location
            }
            
            //获取range
            let range = NSRange(location: location, length: length)
            
            let array = (emoticons as NSArray).subarrayWithRange(range)
            
            tempArray.append(array as! [WBEmoticonModel])
            
        }

        return tempArray
        
    }
    
    
    
    //根据传进来的文件名字 返回一维数组
    func getEmoticonsWithPath(path:String) -> [WBEmoticonModel] {
        
        let file = emoticonBundle.pathForResource("\(path)/info.plist", ofType: nil)
        
        let array = NSArray(contentsOfFile: file!)!
        
        //数组里面存储的是字典
        var tempArray:[WBEmoticonModel] = [WBEmoticonModel]()
        
        //遍历数组 字典转模型
        for dict in  array{
            
            let model = WBEmoticonModel(dict: dict as! [String:AnyObject])
            
            model.path = "\(path)/"
            
            //添加到可变数组中
            tempArray.append(model)
            
        }
        
        return tempArray
        
    }
    
    
    //根据传递进来的字符串查找 返回一个模型
    func searchEmoticonModelWithChsString(chs:String) -> WBEmoticonModel? {
        
        //遍历默认图片表情数组
        for emoticon in defaultEmoticons {
            
            if emoticon.chs == chs{
            
                return emoticon
            
            }
            
        }
        
        //遍历浪小花表情数组
        for emoticon in lxhEmoticons {
            
            if emoticon.chs == chs{
                
                return emoticon
                
            }
            
        }
        
        return nil
        
    }
    
    
}
